package com.msb.servicemap.remote;

import com.msb.commons.constant.AmapConstant;
import com.msb.commons.dto.PointDTO;
import com.msb.commons.dto.Points;
import com.msb.commons.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @Author: chshh
 * @version: 1.0
 */
@Service
public class PointClient {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${amap.key}")
    private String amapKey;

    @Value("${amap.sid}")
    private String amapSid;

    public ResponseResult upload(PointDTO pointDTO) {

        StringBuilder urlBuilder = new StringBuilder();

        urlBuilder.append(AmapConstant.POINT_UPLOAD);
        urlBuilder.append("?");
        urlBuilder.append("key=" + amapKey);
        urlBuilder.append("&");
        urlBuilder.append("sid=" + amapSid);
        urlBuilder.append("&");
        urlBuilder.append("tid=" + pointDTO.getTid());
        urlBuilder.append("&");
        urlBuilder.append("trid=" + pointDTO.getTrid());
        urlBuilder.append("&");
        urlBuilder.append("points=");
        //转义
        urlBuilder.append("%5B");
        for (Points p: pointDTO.getPoints()){
            urlBuilder.append("%7B");

            urlBuilder.append("%22location%22");
            urlBuilder.append("%3A");
            urlBuilder.append("%22"+p.getLocation()+"%22");
            urlBuilder.append("%2C");
            urlBuilder.append("%22locatetime%22");
            urlBuilder.append("%3A");
            urlBuilder.append(p.getLocatetime());
            urlBuilder.append("%7D");
        }
        urlBuilder.append("%5D");
        //调用高德接口
        ResponseEntity<String> result = restTemplate.postForEntity(URI.create(urlBuilder.toString()), null, String.class);
        System.out.println("高德响应数据: "+result);
        return ResponseResult.success();
    }

}
